package com.coai.file.service

import com.coai.file.model.FileMetadata
import org.springframework.kafka.core.KafkaTemplate
import org.springframework.stereotype.Service

/**
 * 文件事件服务 - 发布文件相关事件到Kafka
 * 用于数字孪生数据跟踪
 */
@Service
class FileEventService(
    private val kafkaTemplate: KafkaTemplate<String, Any>
) {
    
    companion object {
        private const val TOPIC_FILE_EVENTS = "file-events"
    }
    
    /**
     * 发布文件上传事件
     */
    fun publishFileUploaded(file: FileMetadata) {
        val event = mapOf(
            "eventType" to "FILE_UPLOADED",
            "fileId" to file.id,
            "fileName" to file.originalName,
            "fileSize" to file.size,
            "mimeType" to file.mimeType,
            "category" to file.category.name,
            "uploaderId" to file.uploaderId,
            "conversationId" to file.conversationId,
            "timestamp" to file.createdAt.toString()
        )
        
        kafkaTemplate.send(TOPIC_FILE_EVENTS, file.id, event)
    }
    
    /**
     * 发布文件下载事件
     */
    fun publishFileDownloaded(file: FileMetadata, userId: String) {
        val event = mapOf(
            "eventType" to "FILE_DOWNLOADED",
            "fileId" to file.id,
            "userId" to userId,
            "downloadCount" to file.downloadCount,
            "timestamp" to System.currentTimeMillis()
        )
        
        kafkaTemplate.send(TOPIC_FILE_EVENTS, file.id, event)
    }
    
    /**
     * 发布文件删除事件
     */
    fun publishFileDeleted(file: FileMetadata) {
        val event = mapOf(
            "eventType" to "FILE_DELETED",
            "fileId" to file.id,
            "fileName" to file.originalName,
            "uploaderId" to file.uploaderId,
            "timestamp" to System.currentTimeMillis()
        )
        
        kafkaTemplate.send(TOPIC_FILE_EVENTS, file.id, event)
    }
}
